package com.tools.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tools.common.enums.ResultCode;
import com.tools.common.result.Result;
import com.tools.pojo.user.bo.UpdateUserPwdBO;
import com.tools.pojo.user.bo.UserBO;
import com.tools.pojo.user.dto.UserDTO;
import com.tools.pojo.user.vo.UserVO;
import com.tools.service.user.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@AllArgsConstructor
@Tag(name = "用户模块",description = "用户模块")
public class UserController {

    private final UserService userService;

    /**
     * 查询用户分页列表
     * @param userDTO 用户查询条件
     * @return 用户分页列表
     */
    @Operation(summary = "查询用户分页列表",description = "查询用户分页列表")
    @GetMapping("/page")
    public Result<IPage<UserVO>> page( UserDTO userDTO) {
        return Result.success(userService.page(userDTO));
    }

    /**
     * 查询用户列表
     * @param userDTO 用户查询条件
     * @return 用户列表
     */
    @Operation(summary = "用户列表",description = "用户列表")
    @GetMapping("/list")
    public Result<List<UserVO>> list(@RequestBody UserDTO userDTO) {
        return Result.success(userService.list(userDTO));
    }

    /**
     * 新增用户
     * @param userBO 用户业务对象
     * @return 新增结果
     */
    @Operation(summary = "新增用户",description = "新增用户")
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody UserBO userBO) {
        Boolean b = userService.saveUser(userBO);
        return b?Result.success():Result.fail(ResultCode.DATA_ADD_ERROR);
    }

    /**
     * 更新用户
     * @param userBO 用户业务对象
     * @return 更新结果
     */
    @Operation(summary = "更新用户",description = "更新用户")
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody UserBO userBO) {
        Boolean b = userService.updateUser(userBO);
        return b?Result.success():Result.fail(ResultCode.DATA_UPDATE_ERROR);
    }

    /**
     * 删除用户
     * @param id 用户ID
     * @return 删除结果
     */
    @Operation(summary = "删除用户",description = "删除用户")
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        boolean b = userService.removeById(id);
        return b?Result.success():Result.fail(ResultCode.DATA_DEL_ERROR);
    }

    /**
     * 重置密码
     * @param id 用户ID
     * @return 重置结果
     */
    @Operation(summary = "重置密码",description = "重置密码")
    @PutMapping("/resetPassword/{id}")
    public Result<Boolean> resetPassword(@PathVariable("id") Long id) {
        boolean b = userService.resetPassword(id);
        return b?Result.success():Result.fail(ResultCode.DATA_RESET_PASSWORD_ERROR);
    }

    /**
     * 修改密码
     * @param updateUserPwdBO 用户业务对象
     * @return 修改结果
     */
    @Operation(summary = "修改密码",description = "修改密码")
    @PostMapping("/updatePassword")
    public Result<Boolean> updatePassword(@RequestBody UpdateUserPwdBO updateUserPwdBO) {
        boolean b = userService.updatePassword(updateUserPwdBO);
        return b?Result.success():Result.fail(ResultCode.DATA_UPDATE_PASSWORD_ERROR);
    }

    /**
     * 根据token查询用户信息
     * @return 用户信息
     */
    @Operation(summary = "根据token查询用户信息",description = "根据token查询用户信息")
    @GetMapping("/getUserInfo")
    public Result<UserVO> getUserInfo() {
        return Result.success(userService.getUserInfo());
    }


    /**
     * 批量删除用户
     */
     @Operation(summary = "批量删除用户",description = "批量删除用户")
    @PostMapping("/batchDeleteUsers")
    public Result<Boolean> batchDeleteUsers(@RequestBody List<Long> ids) {
        boolean b = userService.batchDeleteUsers(ids);
        return b?Result.success():Result.fail(ResultCode.DATA_DEL_ERROR);
    }

}
